package com.whfc.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/10/21 14:34
 */
public class SignUtil {

    private static Logger logger = LoggerFactory.getLogger(SignUtil.class);

    /**
     * 计算签名(物资)
     *
     * @param appId
     * @param diskSn
     * @param mac
     * @param timestamp
     * @param randomStr
     * @param appKey
     * @return
     */
    public static String sign(String appId, String diskSn, String mac, Long timestamp, String randomStr, String appKey) {
        String text = String.format("appId=%s&diskSn=%s&mac=%s&timestamp=%s&randomStr=%s&appKey=%s", appId, diskSn, mac, timestamp, randomStr, appKey);
        String md5 = MD5Util.md5Encode(text);
        logger.info("text:[{}],md5:[{}]", text, md5);
        return md5;
    }

    /**
     * 计算签名(开放接口)(旧)
     *
     * @param appId
     * @param timestamp
     * @param randomStr
     * @param appKey
     * @return
     */
    public static String sign(String appId, Long timestamp, String randomStr, String appKey) {
        String text = String.format("appId=%s&timestamp=%s&randomStr=%s&appKey=%s", appId, timestamp, randomStr, appKey);
        String md5 = MD5Util.md5Encode(text);
        logger.info("text:[{}],md5:[{}]", text, md5);
        return md5;
    }

    /**
     * 计算签名(开放接口)(v1)
     *
     * @param appKey
     * @param timestamp
     * @param randomStr
     * @param appSecret
     * @return
     */
    public static String signV1(String appKey, Long timestamp, String randomStr, String appSecret) {
        String text = String.format("appKey=%s&randomStr=%s&timestamp=%s&appSecret=%s", appKey, randomStr, timestamp, appSecret);
        String md5 = MD5Util.md5Encode(text);
        logger.info("text:[{}],md5:[{}]", text, md5);
        return md5;
    }


    public static String sign(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        logger.info("sign参数,map:{}", map);
        List<String> keys = new ArrayList<>(map.keySet());
        keys.sort(Comparator.naturalOrder());
        StringBuilder sb = new StringBuilder();
        keys.forEach(key -> sb.append(key).append("=").append(map.get(key)).append("&"));
        String signStr = sb.substring(0, sb.length() - 1);
        logger.info("signStr:{}", signStr);
        String sign = MD5Util.md5Encode(signStr);
        logger.info("sign:{}", sign);
        return sign;
    }


    public static void main(String[] args) {
        String appKey = "610fa7895f4dee82258b9944be992465";
        String str = "b5fc99d6680c405e";
        Long timestamp = 1617950347000L;

        System.out.println("timestamp:" + timestamp);
        Map<String, Object> map = new HashMap<>();
        map.put("appKey", appKey);
        map.put("timestamp", timestamp);
        map.put("randomStr", str);
        map.put("deviceCode", "19FC01010001");
        System.out.println("sign-v2:" + sign(map));
    }
}
